package com.et114.modules.cust.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.acegisecurity.providers.encoding.Md5PasswordEncoder;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.et114.core.action.BaseDispatchAction;
import com.et114.modules.cust.form.Mng_CustForm;
import com.et114.modules.cust.form.Mng_LeadForm;
import com.et114.modules.cust.services.IMng_CustService;
import com.et114.modules.cust.services.Mng_CustServiceImp;
import com.et114.modules.cust.vo.CustBaseVo;
import com.et114.modules.cust.vo.CustInfoVo;
import com.et114.modules.cust.vo.LeadVo;
import com.et114.modules.org.services.OrgFactory;
import com.et114.modules.org.services.UserManager;
import com.et114.modules.org.utility.SessionUserInfo;
import com.et114.modules.org.vo.UserVo;

/**
 * 客户管理:信息查询
 * 
 * @author zhiweip
 * @version 1.0
 * @created 09-十一月-2010 23:03:48
 */
public class Mng_CustQueryAction extends BaseDispatchAction {
	
	// userManager
	private IMng_CustService	mng_CustService	= ( Mng_CustServiceImp ) getBean ( "mng_CustService" );
	
	public Mng_CustQueryAction ( ) {
		
	}
	
	public void finalize ( ) throws Throwable {
		
	}
	
	@ Override
	public ActionForward unspecified ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		
		Mng_CustForm cform = ( Mng_CustForm ) form;
		
		request
				.setAttribute (
						"EMPLYS" ,
						( ( com.et114.modules.cust.dao.Mng_CustDaoImp ) getBean ( "mng_CustDao" ) )
								.query_AllEmpList ( null ) );
		
		return mapping.findForward ( "queryCustDataList" );
	}
	
	// Mng_CustForm
	public ActionForward custQuery ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		
		UserManager userManager = ( UserManager ) getBean ( "userManager" );
		UserVo user = new UserVo ( );
		user.setPageSize ( 10000 );
		request
				.setAttribute (
						"EMPLYS" ,
						( ( com.et114.modules.cust.dao.Mng_CustDaoImp ) getBean ( "mng_CustDao" ) )
								.query_AllEmpList ( null ) );
		
		Mng_CustForm cform = ( Mng_CustForm ) form;
		CustBaseVo vo = cform.getCust ( );
		vo.setPageNum ( cform.getPageNum ( ) );
		vo.setPageSize ( cform.getPageSize ( ) );
		request.setAttribute ( "cust" , vo );
		request.setAttribute ( "DataList" , mng_CustService
				.queryCustDataList ( vo ) );
		return mapping.findForward ( "queryCustDataList" );
	}
	
	public ActionForward toExpXls ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception { 
		
		Mng_CustForm cform = ( Mng_CustForm ) form;
		CustBaseVo vo = cform.getCust ( );
		mng_CustService.toExpXls_Cust ( vo,response);
		return null;
	}
	
	public ActionForward custOrd ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		
		UserManager userManager = ( UserManager ) getBean ( "userManager" );
		UserVo user = new UserVo ( );
		user.setPageSize ( 10000 );
		request
				.setAttribute (
						"EMPLYS" ,
						( ( com.et114.modules.cust.dao.Mng_CustDaoImp ) getBean ( "mng_CustDao" ) )
								.query_AllEmpList ( null ) );
		request.setAttribute ( "OPTIONS" , mng_CustService.getItemTypes ( ) ) ;
		Mng_CustForm cform = ( Mng_CustForm ) form; 
		
		return mapping.findForward ( "queryCustOrdDataList" );
	}
	public ActionForward custOrdQuery ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		
		UserManager userManager = ( UserManager ) getBean ( "userManager" );
		UserVo user = new UserVo ( );
		user.setPageSize ( 10000 );
		request
				.setAttribute (
						"EMPLYS" ,
						( ( com.et114.modules.cust.dao.Mng_CustDaoImp ) getBean ( "mng_CustDao" ) )
								.query_AllEmpList ( null ) );
		request.setAttribute ( "OPTIONS" , mng_CustService.getItemTypes ( ) ) ; 
		Mng_CustForm cform = ( Mng_CustForm ) form;
		CustBaseVo vo = cform.getCust ( );
		vo.setPageNum ( cform.getPageNum ( ) );
		vo.setPageSize ( cform.getPageSize ( ) );
		request.setAttribute ( "cust" , vo );
		request.setAttribute ( "DataList" , mng_CustService
				.query_custOrdDataList ( vo ) );
		return mapping.findForward ( "queryCustOrdDataList" );
	}
	
	public ActionForward toExpOrdXls ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception { 
		
		Mng_CustForm cform = ( Mng_CustForm ) form;
		CustBaseVo vo = cform.getCust ( );
		mng_CustService.toExpOrdXls_Cust ( vo,response);
		return null;
	}
	
	public ActionForward toOrder ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		UserManager userManager = ( UserManager ) getBean ( "userManager" );
		UserVo user = new UserVo ( );
		user.setPageSize ( 10000 );
		request
				.setAttribute (
						"EMPLYS" ,
						( ( com.et114.modules.cust.dao.Mng_CustDaoImp ) getBean ( "mng_CustDao" ) )
								.query_AllEmpList ( null ) );
		
		Mng_CustForm cform = ( Mng_CustForm ) form;
		CustBaseVo vo = cform.getCust ( );
		vo.setPageNum ( cform.getPageNum ( ) );
		vo.setPageSize ( cform.getPageSize ( ) );
		request.setAttribute ( "cust" , vo );
		// vo.setCusttype ( BIZ.CUST_LEAD );
		request.setAttribute ( "DataList" , mng_CustService
				.queryCustDataList ( vo ) );
		return mapping.findForward ( "toOrderCustDataList" );
	}
 
	public ActionForward doUdtOrd ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception { 
		Mng_CustForm cform = ( Mng_CustForm ) form;
		CustBaseVo cust = cform.getCust ( );
		SessionUserInfo sessionuserinfo = getCurrentUser ( request );
		cust.setCrtUser ( sessionuserinfo.getUserId ( ) );
		cust.setUpdtUser ( sessionuserinfo.getUserId ( ) );
		mng_CustService.update_Pub_Cust_By_orderstate ( cust );
		request.setAttribute ( "back" , "Mng_CustQueryAction.go?method=custOrd" );
		return mapping.findForward ( "success" );
	}
	
	public ActionForward leadsQuery ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		
		Mng_CustForm cform = ( Mng_CustForm ) form;
		
		return mapping.findForward ( "queryLeadDataList" );
	}
	
	public ActionForward toModify ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		
		Mng_CustForm cform = ( Mng_CustForm ) form;
		request
				.setAttribute (
						"EMPLYS" ,
						( ( com.et114.modules.cust.dao.Mng_CustDaoImp ) getBean ( "mng_CustDao" ) )
								.query_AllEmpList ( null ) );
		
		String custid = request.getParameter ( "custid" );
		if ( custid != null && ! custid.trim ( ).equals ( "" ) ) {
			CustBaseVo cust = new CustBaseVo ( );
			cust.setCustid ( new Integer ( custid ) );
			cust.setOperType ( "edit" );
			CustInfoVo info = new CustInfoVo ( );
			info.setCustid ( cust.getCustid ( ) );
			
			request.setAttribute ( "BASEINFO" , mng_CustService
					.loadCust ( cust ) );
			info.setOperType ( "edit" );
			List list = mng_CustService.loadCustMsgByType ( info );
			request.setAttribute ( "INFOLIST" , list );
		} else {
			request.setAttribute ( "ISUPDATE" , "no" );
		}
		
		return mapping.findForward ( "toModify" );
	}
	
	public ActionForward toPwd ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		String custids = request.getParameter ( "custids" );
		//System.out.println ("++++++++++++++++++++"+custids);
		request.setAttribute ( "CUSTIDS" , custids );
		return mapping.findForward ( "toPwd" );
	}
	public ActionForward udpPwd ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		Md5PasswordEncoder md5PasswordEncoder = new Md5PasswordEncoder ( );
		String pwd = request.getParameter ( "pwd" );
		String custid = request.getParameter ( "custids" );
		//System.out.println ("++++++++++++++++++++"+custid);
		if ( custid != null ) {
			String [ ] custids = custid.split ( ";" );
			for ( String id : custids ) {
				CustBaseVo cvo = new CustBaseVo ( );
				cvo.setCustid ( new Integer ( id ) );
				cvo = mng_CustService.loadCust ( cvo );
				if ( cvo.getReguserid ( ) != null ) {
					UserVo uservo = OrgFactory.getUserManager ( ).getUserById (
							new Long(cvo.getReguserid ( )) );
					
					if ( uservo != null ) {
						uservo.setPassword ( md5PasswordEncoder.encodePassword (
								pwd , null ) );
						OrgFactory.getUserManager ( ).updateUser ( uservo );
					}
					
				}
			}
		}
		request.setAttribute ( "SAVEFLAG" , "OK" );
		return mapping.findForward ( "toPwd" );
	}
	
	/**
	 * 更改客户专员
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward changeEmp ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		
		String custids = request.getParameter ( "custids" );
		request.setAttribute ( "CUSTIDS" , custids );
		UserManager userManager = ( UserManager ) getBean ( "userManager" );
		UserVo user = new UserVo ( );
		user.setPageSize ( 10000 );
		request
				.setAttribute (
						"EMPLYS" ,
						( ( com.et114.modules.cust.dao.Mng_CustDaoImp ) getBean ( "mng_CustDao" ) )
								.query_AllEmpList ( null ) );
		return mapping.findForward ( "changeEmp" );
	}
	
	public ActionForward saveChange ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		
		Mng_CustForm cform = ( Mng_CustForm ) form;
		LeadVo cust = cform.getCust ( );
		SessionUserInfo sessionuserinfo = getCurrentUser ( request );
		cust.setCrtUser ( sessionuserinfo.getUserId ( ) );
		cust.setUpdtUser ( sessionuserinfo.getUserId ( ) );
		
		String custid = request.getParameter ( "custids" );
		
		if ( custid == null ) {
			request.setAttribute ( "msg" , "关键字丢失!" );
			request.setAttribute ( "close" , "yes" );
			return mapping.findForward ( "info" );
		}
		String [ ] custids = custid.split ( ";" );
		mng_CustService.updateLeaderCustEmp ( cust , custids );
		request.setAttribute ( "SAVEFLAG" , "OK" );
		return mapping.findForward ( "changeEmp" );
	}
	
	public ActionForward selectCustInfo ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		CustInfoVo info = new CustInfoVo ( );
		String custid = request.getParameter ( "custid" );
		String dtype = request.getParameter ( "dtype" );
		info.setCustid ( new Integer ( custid ) );
		info.setDtype ( dtype );
		List list = mng_CustService.loadCustMsgByType ( info );
		request.setAttribute ( "INFOLIST" , list );
		request.setAttribute ( "DTYPE" , dtype );
		
		return mapping.findForward ( "CustInfo" );
		
	}
	
	/**
	 * 客户详细信息
	 */
	public ActionForward view ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		Mng_CustForm cform = ( Mng_CustForm ) form;
		String custid = request.getParameter ( "custid" );
		
		CustBaseVo cust = new CustBaseVo ( );
		cust.setCustid ( new Integer ( custid ) );
		CustInfoVo info = new CustInfoVo ( );
		info.setCustid ( cust.getCustid ( ) );
		
		request.setAttribute ( "BASEINFO" , mng_CustService.loadCust ( cust ) );
		List list = mng_CustService.loadCustMsgByType ( info );
		request.setAttribute ( "INFOLIST" , list );
		// CustDetail
		return mapping.findForward ( "CustDetail" );
	}
	
}